package com.autoglm.android.core.models

/**
 * 矩形边界数据类
 */
data class Rect(
    val left: Int,
    val top: Int,
    val right: Int,
    val bottom: Int
) {
    /**
     * 获取宽度
     */
    fun width(): Int = right - left
    
    /**
     * 获取高度
     */
    fun height(): Int = bottom - top
    
    /**
     * 获取面积
     */
    fun area(): Int = width() * height()
    
    /**
     * 判断是否为空矩形
     */
    fun isEmpty(): Boolean = width() <= 0 || height() <= 0
    
    /**
     * 判断矩形是否包含指定点
     */
    fun contains(x: Int, y: Int): Boolean =
        x >= left && x <= right && y >= top && y <= bottom
    
    /**
     * 判断矩形是否包含另一个矩形
     */
    fun contains(rect: Rect): Boolean =
        left <= rect.left && top <= rect.top && right >= rect.right && bottom >= rect.bottom
    
    /**
     * 判断两个矩形是否相交
     */
    fun intersects(rect: Rect): Boolean =
        !(left > rect.right || right < rect.left || top > rect.bottom || bottom < rect.top)
    
    /**
     * 获取两个矩形的相交部分
     */
    fun intersection(rect: Rect): Rect {
        val l = maxOf(left, rect.left)
        val t = maxOf(top, rect.top)
        val r = minOf(right, rect.right)
        val b = minOf(bottom, rect.bottom)
        
        return if (l <= r && t <= b) {
            Rect(l, t, r, b)
        } else {
            // 不相交则返回空矩形
            Rect(0, 0, 0, 0)
        }
    }
    
    /**
     * 计算与另一个矩形的相交面积
     */
    fun intersectionArea(rect: Rect): Int {
        val intersection = intersection(rect)
        return intersection.area()
    }
    
    /**
     * 转换为Android原生Rect
     */
    fun toAndroidRect(): android.graphics.Rect = 
        android.graphics.Rect(left, top, right, bottom)
    
    companion object {
        /**
         * 从Android原生Rect创建
         */
        fun fromAndroidRect(rect: android.graphics.Rect): Rect =
            Rect(rect.left, rect.top, rect.right, rect.bottom)
            
        /**
         * 创建空矩形
         */
        fun empty(): Rect = Rect(0, 0, 0, 0)
    }
} 